var API = require('API').survey.temp;

var cols = [
    {
	field : 'create_time' , 
	headerName: 'create_time' , 
	filterParams:{ newRowsAction: 'keep' } 
},

{
	field : 'name' , 
	headerName: 'name' , 
	filterParams:{ newRowsAction: 'keep' } 
},

{
	field : 'id' , 
	headerName: 'id' , 
	filterParams:{ newRowsAction: 'keep' } 
},

    {
        headerName  : '操作',
        cellRenderer: function (p) {
            var container = document.createElement('span');

            var open = document.createElement('a');
            open.innerHTML = '<i class="fa fa-folder-open" style="padding:0 .25em" title="打开"></i>';
            open.addEventListener('click', function(){
                callbacks.open(p.data)
            });

            var _export = document.createElement('a');
            _export.innerHTML = '<i class="fa fa-upload" style="padding:0 .25em;color:darkred" title="删除"></i>';
            _export.addEventListener('click', function(){
                    callbacks.export(p.data);
            });


            var del = document.createElement('a');
            del.innerHTML = '<i class="fa fa-trash-o" style="padding:0 .25em;color:darkred" title="删除"></i>';
            del.addEventListener('click', function(){
                if(confirm("确定要删除？")){
                    callbacks.del(p.data);
                }
            });

            container.appendChild(open);
            container.appendChild(_export);
            container.appendChild(del);

            return container;
        },
        suppressMenu:true,
        suppressSorting:true
    }
];

var getData = function () {

    return {
        getRows : function (p) {
            var qo = {};

            //pagination
            qo.offset = p.startRow;
            qo.limit = p.endRow - p.startRow;

            //sort
            if (p.sortModel.length > 0) {
                qo.sort = '';
                p.sortModel.forEach(function (val) {
                    qo.sort += (val.sort == 'desc' ? '-' : '+') + val.colId + ", ";
                });
            }
            //filter
            var search = p.filterModel;

            if (search) {
                Object.keys(search).forEach(function (val) {
                    var searchObj = search[val];
                    switch (searchObj.type){
                        case 'set':
                            qo[val] = "in," + searchObj.values.join(',');
                            break;
                        case 1:
                            qo[val] = "lk,%" + searchObj.filter + "%";
                            break;
                        case 2:
                            qo[val] = searchObj.filter;
                            break;
                    }
                });
            }
            API.search(qo, function (json, status, xhr) {
                var count = xhr.getResponseHeader('X-Total-Count');
                p.successCallback(json, Number(count));
            });
        },
        pageSize: 50

    }
};
var callbacks = {
    "open": function(){alert('open')},
    "del": function(){alert('del')},
    "export": function(){alert('export')}
    //"statusChanged":function(){alert('statusChanged')}
};

var t = {
    grid  : {
        //angularCompileRows     : true,
        columnDefs             : cols,
        rowData                : [],
        datasource             : getData(),
        rowModelType           : 'pagination',
        enableServerSideSorting: true,
        enableServerSideFilter : true,
        suppressCellSelection  : true,
        rowHeight              : 22,
        colWidth               : 100,
        enableColResize        : true,
        enableCellExpressions  : true,
        api                    : {},
        columnApi              : {},
        rowSelection           : 'single',
        localeText             : require('locale').aggrid
    },
    refresh: function () {
        t.grid.api.setDatasource(getData());
    },
    on: function(name, cb){
        callbacks[name] = cb;
    }
};

module.exports = t;
